package library.common.util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

/**
 * @author Created by qiang.hou on 2018/10/10.
 * @version 1.0
 */
public class GsonUtils {

    public static <T> T fromJson(String json, Class<T> classOfT) {
        Gson gson = new Gson();
        return gson.fromJson(json, classOfT);
    }

    public static <T> T fromJson(String json, Type type) {
        Gson gson = new Gson();
        return gson.fromJson(json, type);
    }

    public <T> String listToJson(List<T> list) {
        Gson gson = new Gson();
        return gson.toJson(list);
    }

    /**
     * json to list<Object>
     *
     * @param json json string
     * @return list<Object>
     */
    public static List<Object> jsonToList(String json) {
        if (null == json) {
            return null;
        }
        return new Gson().fromJson(json, new TypeToken<List<Object>>() {
        }.getType());
    }
}
